package com.example.eos.controller;

import com.example.eos.bean.Comment;
import com.example.eos.bean.ReturnInfo;
import com.example.eos.service.CommentService;
import com.example.eos.service.GlobalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class CommentController {

    private CommentService commentService;
    private GlobalService globalService;

    @Autowired
    public CommentController(CommentService commentService, GlobalService globalService){
        this.commentService = commentService;
        this.globalService = globalService;
    }

    //查看评论
    @RequestMapping(value = "/comment/{commentid}",method = RequestMethod.GET)
    public ReturnInfo getComment(@PathVariable String commentid){
        return commentService.getComment(commentid);
    }

    //查看评论列表
    @RequestMapping(value = "/comments/{publishid}",method = RequestMethod.GET)
    public ReturnInfo getComments(@PathVariable String publishid){
        return commentService.getComments(publishid);
    }

    //新建评论
    @RequestMapping(value = "/comment",method = RequestMethod.POST)
    public ReturnInfo saveComment(@RequestBody @Valid Comment comment, BindingResult result){
        List<String> stringList = new ArrayList<>();
        if(result.hasErrors()){
            return globalService.returnErrors(result, stringList);
        }else {
            return commentService.saveComment(comment, stringList);
        }
    }

    //删除评论
    @RequestMapping(value = "/comment/{commentid}", method = RequestMethod.PUT)
    public ReturnInfo deteleComment(@PathVariable String commentid){
        return commentService.deteleComment(commentid);
    }

}
